package com.hx.control;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hx.entity.Courses;
import com.hx.entity.InfoManage;
import com.hx.entity.Major;
import com.hx.entity.SurveyDetail;
import com.hx.entity.SurveyItems;
import com.hx.entity.Teacher;
import com.hx.service.CourseService;
import com.hx.service.InfoManageService;
import com.hx.service.MajorService;
import com.hx.service.SurveyDetailService;
import com.hx.service.SurveyItemService;

@RestController  /*所有方法返回的都是json对象*/
//@RequestMapping("")
public class Survey {
	
	@Autowired              /*带有组件@Component才可以实例化*/
	private MajorService majorService;          
	@Autowired
	private CourseService courseService;
	@Autowired
	private SurveyItemService surveyItemService;
	@Autowired
	private InfoManageService infoService;
	@Autowired
	private SurveyDetailService detailService;

	
	
	@RequestMapping("/getmajor")   /*请求专业*/
	public List<Major> getMajor(){
		return majorService.getAll();		
	}
	
	@RequestMapping("/getcourse/{id}")      /*请求课程*/
	public List<Courses> getCourses(@PathVariable("id") Integer id){
		return majorService.getOne(id).getCous();
	}
	
	@RequestMapping("/getteacher/{id}")  /*请求教师*/
	public List<Teacher> getTeacher(@PathVariable("id") Integer id){
		return courseService.getOne(id).getTeas();
	}
	
	@RequestMapping("/getsurveyitems")   /*请求调查项目内容*/
	public List<SurveyItems> getSurveyItems(){
		List<SurveyItems> surveyItems=surveyItemService.getAll();
		 for (SurveyItems s : surveyItems) {
			s.getConts();
		}
		 return surveyItems;
	}
	
	
	
	@RequestMapping(value="/savesurvey",method=RequestMethod.POST)   /*前端提交到savesurvey*/
	public String saveSurvey(HttpServletRequest request, @RequestParam("content_id") Integer[] ids,
			@RequestParam("score") Integer[] scores,@RequestParam("major_id") Integer major_id,
			@RequestParam("courses_Id") Integer course_id,@RequestParam("teacher_Id") Integer teacher_id,
			@RequestParam("date") String date,@RequestParam("suggest") String suggest){
		HttpSession session=request.getSession();  //给评价的学生一个编号
		InfoManage infoManage=new InfoManage();
		infoManage.setSession_Id(session.getId());
		infoManage.setMajor_id(major_id);
		infoManage.setCourses_Id(course_id);		
		infoManage.setDate(date);
		infoManage.setTeacher_Id(teacher_id);
		infoManage.setSuggest(suggest);
		int sum=0;
		for(int s:scores) sum+=s;
		infoManage.setScore_Sum(sum);
//		infoManage.setDetails(new ArrayList<SurveyDetail>());
		infoService.save(infoManage);	
		for(int i=0;i<ids.length;i++){
			SurveyDetail sd=new SurveyDetail();
			sd.setContent_Id(ids[i]);
			sd.setScore(scores[i]);
			sd.setInfos(infoManage);
			detailService.save(sd);
		}		
		return "提交成功";
	}
}
